/*
 *	MidiInDump.java
 *
 *	This file is part of jsresources.org
 */

/*
 * Copyright (c) 1999 - 2001 by Matthias Pfisterer
 * Copyright (c) 2003 by Florian Bomers
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
|<---            this code is formatted to fit into 80 columns             --->|
*/

import java.io.IOException;

import javax.sound.midi.Transmitter;
import javax.sound.midi.Receiver;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Synthesizer;

/*	If the compilation fails because this class is not available,
	get gnu.getopt from the URL given in the comment below.
*/
import gnu.getopt.Getopt;


@SuppressWarnings("unused")
public class MidiInDump
{

	private static boolean		DEBUG = true;

	public static void main(String strDeviceName,int nDeviceIndex)
		throws Exception
	{

		/*
		 *	The device name/index to listen to.
		 */
		boolean bUseDefaultSynthesizer = false;
		// TODO: synchronize options with MidiPlayer


		if ((strDeviceName == null) && (nDeviceIndex < 0))
			{
				out("device name/index not specified!");
				printUsageAndExit();
			}

		MidiDevice.Info	info;
		if (strDeviceName != null) 
			{
				info = MidiCommon.getMidiDeviceInfo(strDeviceName, false);
				System.out.println("strDeviceName");
				
			}
		else
			{
				info = MidiCommon.getMidiDeviceInfo(nDeviceIndex);
				System.out.println("index");
			}
		if (info == null)
			{
				if (strDeviceName != null) 
					{
						out("no device info found for name " + strDeviceName);
					}
				else
					{
						out("no device info found for index " + nDeviceIndex);
					}
				System.exit(1);
			}
		else {
//			System.out.println("1");
		}

		MidiDevice inputDevice = null;
		try
			{
				inputDevice = MidiSystem.getMidiDevice(info);
//				System.out.println("2");
				inputDevice.open();
				System.out.println(inputDevice.getDeviceInfo().getName());
			}
		catch (MidiUnavailableException e)
			{
				out(e);
			}
		if (inputDevice == null)
			{
				out("wasn't able to retrieve MidiDevice");
				System.exit(1);
			}
		Receiver r = new DumpReceiver(System.out);
		try
			{
				Transmitter	t = inputDevice.getTransmitter();
				t.setReceiver(r);
			}
		catch (MidiUnavailableException e)
			{
				out("wasn't able to connect the device's Transmitter to the Receiver:");
				out(e); 
				inputDevice.close();
				System.exit(1);
			}
		if (bUseDefaultSynthesizer)
			{
			    Synthesizer synth = MidiSystem.getSynthesizer();
			    synth.open();
			    r = synth.getReceiver();
				try
					{
						Transmitter	t = inputDevice.getTransmitter();
						t.setReceiver(r);
					}
				catch (MidiUnavailableException e)
					{
						out("wasn't able to connect the device's Transmitter to the default Synthesizer:");
						out(e); 
						inputDevice.close();
						System.exit(1);
					}
			}
		out("now running; interupt the program with [ENTER] when finished");

		try
			{
				System.in.read();
			}
		catch (IOException ioe)
			{
			}
		inputDevice.close();
		out("Received "+((DumpReceiver) r).seCount+" sysex messages with a total of "+((DumpReceiver) r).seByteCount+" bytes");
		out("Received "+((DumpReceiver) r).smCount+" short messages with a total of "+((DumpReceiver) r).smByteCount+" bytes");
		out("Received a total of "+(((DumpReceiver) r).smByteCount + ((DumpReceiver) r).seByteCount)+" bytes");
		try
			{
				Thread.sleep(1000);
			}
		catch (InterruptedException e)
			{
				if (DEBUG) { out(e); }
			}
	}



	private static void printUsageAndExit()
	{
		out("MidiInDump: usage:");
		out("  java MidiInDump -h");
		out("    gives help information");
		out("  java MidiInDump -l");
		out("    lists available MIDI devices");
		out("  java MidiInDump [-D] [-d <input device name>] [-n <device index>]");
		out("    -d <input device name>\treads from named device (see '-l')");
		out("    -n <input device index>\treads from device with given index(see '-l')");
		out("    -D\tenables debugging output");
		System.exit(1);
	}



	private static void out(String strMessage)
	{
		System.out.println(strMessage);
	}



	private static void out(Throwable t)
	{
		if (DEBUG) {
			t.printStackTrace();
		} else {
			out(t.toString());
		}
	}
}



/*** MidiInDump.java ***/